package com.qianbao.qianbaobusiness.ui.mine.qrcode;

import android.support.annotation.NonNull;

import com.androidbase.base.mvp.RxMvpPresenter;
import com.androidbase.download.DownloadException;
import com.androidbase.download.DownloadManager;
import com.androidbase.download.downinterfaceimpl.SimpleDownLoadCallBack;
import com.androidbase.utils.RxUtils;
import com.baseandroid.config.Constant;
import com.baseandroid.config.MyApi;
import com.baseandroid.retrofit.RxObserver;
import com.baseandroid.retrofit.json.Data;
import com.qianbao.qianbaobusiness.json.AliPayResultBean;
import com.qianbao.qianbaobusiness.json.ApplyDownLoadCount;
import com.qianbao.qianbaobusiness.json.CheckUpdate;
import com.qianbao.qianbaobusiness.json.QRCardPayBean;
import com.qianbao.qianbaobusiness.json.SaveQrcodeBean;

import java.io.File;
import java.util.HashMap;

import javax.inject.Inject;

public class ApplyQrcodePresenter extends RxMvpPresenter<ApplyQrCardView> {
    MyApi myApi;

    @Inject
    public ApplyQrcodePresenter(MyApi myApi) {
        this.myApi = myApi;
    }

    /**
     * 根据类型商户编号获取需要保存的二维码图片地址
     *
     * @param merchantNo
     */
    public void saveQrcode(String merchantNo) {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("type", Constant.HISTORY_TYPE);
        hashMap.put("merchantNo", merchantNo);
        myApi.saveQrcode(hashMap)
                .compose(RxUtils.applySchedulersLifeCycle(getView()))
                .subscribe(new RxObserver<Data<SaveQrcodeBean>>() {
                    @Override
                    public void onComplete() {

                    }

                    @Override
                    public void onNext(Data<SaveQrcodeBean> stringData) {
                        if (RxObserver.checkJsonCode(stringData, true)) {
                            getView().onSuccess(stringData.getResult());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                });
    }

    public void requestDownload(String sUpdateUrl, String downLoadFolder, String downLoadFileName) {
        getView().showLoadingDialog();
        DownloadManager.getInstance()
                .download(sUpdateUrl, new File(downLoadFolder), downLoadFileName, false, new SimpleDownLoadCallBack() {

                    @Override
                    public void onProgress(long finished, long total, int progress) {
                    }

                    @Override
                    public void onCompleted(File downloadfile) {
                        if (getView() != null) {
                            getView().downloadCompleted();
                            getView().closeLoadingDialog();
                        }
                    }

                    @Override
                    public void onFailed(DownloadException e) {
                        if (getView() != null) {
                            getView().downloadFailed();
                            getView().closeLoadingDialog();
                        }
                    }
                });
    }

    /**
     * 创建二维码寄送订单
     *
     * @param userNo
     * @param merchantNo
     * @param posMerchantNo
     * @param defaultQrMoney
     * @param mName
     * @param name
     * @param name1
     * @param name2
     * @param mAddrDetail
     */
    public void createOrder(String userNo, String merchantNo, String posMerchantNo
            , String defaultQrMoney, String mName, String phoneNum, String name, String name1
            , String name2, String mAddrDetail) {
        HashMap<String, String> parms = new HashMap<>();
        parms.put("userNo", userNo);
        parms.put("merchantNo", merchantNo);
        parms.put("posNo", posMerchantNo);
        parms.put("orderAmount", defaultQrMoney);
        parms.put("mailAddressee", mName);
        parms.put("mailProvince", name);
        parms.put("mailCity", name1);
        parms.put("mailMobile", phoneNum);
        parms.put("mailDistrict", name2);
        parms.put("mailDetailedAddress", mAddrDetail);
        myApi.createOrder(parms).compose(RxUtils.applySchedulersLifeCycle(getView()))
                .subscribe(new RxObserver<Data<QRCardPayBean>>() {
                    @Override
                    public void onComplete() {

                    }

                    @Override
                    public void onNext(Data<QRCardPayBean> stringData) {
                        if (RxObserver.checkJsonCode(stringData, false)) {
                            getView().creatOrderSuccess(stringData.getResult());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }
                });
    }

    /**
     * 查询剩余下载次数
     */
    public void getQrcodeDownloadCount() {
        myApi.getQrcodeDownloadCount()
                .compose(RxUtils.applySchedulersLifeCycle(getView()))
                .subscribe(new RxObserver<Data<ApplyDownLoadCount>>() {
                    @Override
                    public void onNext(Data<ApplyDownLoadCount> data) {
                        if (RxObserver.checkJsonCode(data, true)) {
                            getView().getDownLoadCount(data.getResult());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }
                });
    }

    /**
     * 根据订单id查询订单支付状态
     *
     * @param id
     */
    public void getOrderById(String id) {
        myApi.getOrderById(id)
                .compose(RxUtils.applySchedulersLifeCycle(getView()))
                .subscribe(new RxObserver<Data<AliPayResultBean>>() {
                    @Override
                    public void onNext(Data<AliPayResultBean> stringData) {
                        if (RxObserver.checkJsonCode(stringData, false)) {
                            getView().getOrderStatus(stringData.getResult());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }
                });
    }

    /**
     * 获取寄送价格
     */
    public void checkUpdate() {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("classcode", Constant.BUSINESS_OUTSIDE_PARAM);
        hashMap.put("paramkey", Constant.QRCODE_ACCOUNT);

        myApi.checkUpdate(hashMap)
                .compose(RxUtils.<Data<CheckUpdate>>applySchedulersLifeCycle(getView()))
                .subscribe(new RxObserver<Data<CheckUpdate>>() {

                    @Override
                    public void onNext(@NonNull Data<CheckUpdate> checkUpdateData) {
                        if (RxObserver.checkJsonCode(checkUpdateData, true)) {
                            getView().OnVersionUpdate(checkUpdateData.getResult());
                        } else {
                            getView().OnVersionUpdateError();
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        getView().OnVersionUpdateError();
                    }
                });
    }
}
